<?php
namespace app\admin\controller;

use think\Db;

class Link extends Base
{
	public function link()
	{
        if ($this->request->isPost() && $this->request->isAjax()) {
            // 查询
            $map = [];

            $class_id = $this->request->param('class_id/s', '');
            if ('' !== $class_id) {
                $map[] = ['class_id', '=', $class_id];
            }

            $name = $this->request->param('name/s', '');
            if ('' !== $name) {
                $map[] = ['name', 'like', '%'.$name.'%'];
            }

            // 分页
            $page = $this->request->param('page/d', 0);
            $limit = $this->request->param('limit/d', 0);
            $count = Db::table('link')->where($map)->count();
            
            $data = Db::table('link')->where($map)->limit($limit * ($page - 1), $limit)->order('id desc')->select();

            if ($data) {
                $link_class = Db::table('link_class')->column('name', 'id');
                foreach ($data as $k => &$v) {
                    $v['class_name'] = array_key_exists($v['class_id'], $link_class) ? $link_class[$v['class_id']] : '';
                }
            }

            // 返回数据
            $res = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $data,
            ];

            return json($res);
        }

        $this->assign('link_class', Db::table('link_class')->column('name', 'id'));

		return $this->fetch();
	}

    public function linkSave()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);
            $data = [
                'class_id'      =>  $this->request->param('class_id/d', 0),
                'name'          =>  trim($this->request->param('name/s', '')),
                'url'           =>  trim($this->request->param('url/s', '')),
                'pic'           =>  trim($this->request->param('pic/s', '')),
                'sort'          =>  $this->request->param('sort/d', 0),
                'switch'        =>  $this->request->param('switch/d', 0),
            ];

            // 校验重复
            $map = [];
            $map[] = ['id', '<>', $id];
            $map[] = ['class_id', '=', $data['class_id']];
            $map[] = ['name', '=', $data['name']];
            if (Db::table('link')->where($map)->count()) {
                $this->result('', 1, '同类名称重复');
            }

            // 添加编辑状态检测
            if ($id) {
                if (!Db::table('link')->where('id', $id)->update($data)) {
                    $this->result('', 1, '编辑链接失败');
                }
            } else {
                if (!Db::table('link')->insert($data)) {
                    $this->result('', 1, '添加链接失败');
                }
            }

            $this->result('', 0, '保存成功');
        }
    }    

    public function linkDel()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);

            if (!Db::table('link')->delete($id)) {
                $this->result('', 1, '删除失败');
            }

            $this->result('', 0, '删除成功');
        }
    }

    public function linkSort()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            if (!Db::table('link')->where('id', $this->request->param('id/d', 0))->setField('sort', $this->request->param('sort/d', 0))) {
                $this->result('', 1, '排序失败');
            }

            $this->result('', 0, '排序成功');
        } 
    }

    public function linkswitch() 
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            if (!Db::table('link')->where('id', $this->request->param('id/d', 0))->setField('switch', $this->request->param('menu/d', 0))) {
                $this->result('', 1, '设置失败');
            }

            $this->result('', 0, '设置成功');
        }
    }

	public function linkClass()
	{
        if ($this->request->isPost() && $this->request->isAjax()) {
            $link_class = Db::table('link_class')->order('id desc')->select();

            // 返回数据
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => count($link_class),
                'data'  => $link_class,
            ];

            return json($data);
        }

		return $this->fetch();
	}

    public function linkClassSave()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);
            $data = [
                'name'  =>  trim($this->request->param('name/s', '')),
            ];

            // 校验重复
            $map = [];
            $map[] = ['id', '<>', $id];
            $map[] = ['name', '=', $data['name']];
            if (Db::table('link_class')->where($map)->count()) {
                $this->result('', 1, '分类名称重复');
            }

            // 添加编辑状态检测
            if ($id) {
                if (!Db::table('link_class')->where('id', $id)->update($data)) {
                    $this->result('', 1, '编辑分类失败');
                }
            } else {
                if (!Db::table('link_class')->insert($data)) {
                    $this->result('', 1, '添加分类失败');
                }
            }

            $this->result('', 0, '保存成功');
        }
    }

    public function linkClassDel()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);
            if (Db::table('link')->where('class_id', $id)->count()) {
                $this->result('', 1, '删除失败，分类下存在链接');
            }

            if (!Db::table('link_class')->delete($id)) {
                $this->result('', 1, '删除失败');
            }

            $this->result('', 0, '删除成功');
        }
    }
}
